package controller

import (
	"mini-app/internal/app/grpc/common"
	"mini-app/internal/app/web/service"
	"strconv"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
)

type Goods struct{}

// ClassList 商品展示分类
func (Goods) ClassList(c *gin.Context) {
	robotID := c.Query("robot_id")
	list, _ := service.Goods{}.ClassList(c.Request.Context(), robotID)
	response.JSON(c, list, nil)
}

// TagList 标签
func (Goods) TagList(c *gin.Context) {
	list, _ := common.GRPC{}.GetTagList(c.Request.Context(), "")
	response.JSON(c, list, nil)
}

// CornerLabelList 脚标
func (Goods) CornerLabelList(c *gin.Context) {
	list, _ := common.GRPC{}.GetCornerLabelList(c.Request.Context(), "")
	response.JSON(c, list, nil)
}

// PickupTypeList 取货方式
func (Goods) PickupTypeList(c *gin.Context) {
	pickupType := map[int]string{
		1: "站点自提",
		2: "包邮",
	}
	response.JSON(c, pickupType, nil)
}

// 商品列表
func (Goods) List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	list, total, err := service.Goods{}.List(
		c.Request.Context(),
		c.Query("robot_id"),
		c.Query("class_id"),
		page,
		limit,
	)
	response.JSON(c, gin.H{
		"total": total,
		"list":  list,
	}, err)
}

// 商品详情
func (Goods) Detail(c *gin.Context) {
	goodsID := c.Query("goods_id")
	robotID := c.Query("robot_id")
	detail, err := service.Goods{}.Detail(c.Request.Context(), goodsID, robotID)
	response.JSON(c, detail, err)
}

// Review 商品评论记录
func (Goods) Review(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	goodsID := c.Query("goods_id")
	list, total := service.Goods{}.Review(c.Request.Context(), goodsID, page, limit)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, nil)
}

// LatestOrder 最近购买记录
func (Goods) LatestOrder(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	goodsID := c.Query("goods_id")
	list, total := service.Goods{}.LatestOrder(c.Request.Context(), goodsID, page, limit)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, nil)
}
